import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yybb_flutter/Macros/yyb_macros.dart';
import 'package:yybb_flutter/http/api_response.dart';
import 'package:yybb_flutter/modules/abnormal_order/models/yybb_abnormal_order_model.dart';
import 'package:yybb_flutter/modules/abnormal_order/vmodel/abnormal_order_vmodel.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_Widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_loading_nodata_widget.dart';
import 'package:yybb_flutter/yybbtools/yybb_services.dart';
import 'package:yybb_flutter/yybbtools/yybb_tools.dart';
import 'abnormal_orderdetail_page.dart';

class AbnormalOrderPage extends StatefulWidget {
  @override
  _AbnormalOrderPageState createState() => _AbnormalOrderPageState();
}

class _AbnormalOrderPageState extends State<AbnormalOrderPage> {
  List _dataList = [];
  var _page = 1;
  RefreshController _refreshController =  RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
     _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading:
        IconButton(
          icon:
          ImageIcon(
            AssetImage("images/public/app_back_btn@3x.png"),
            size: 17,
            color: Colors.black,
          ),
          // new Icon(Icons.arrow_back_ios,color: Colors.red,),
          onPressed: (){
            FlutterBoost.singleton
                .close("sliverListPage",
                exts: {"animated": true});
          },
        ),

        shadowColor:Colors.transparent,
        title:Text(
            "异常订单",
            style: TextStyle(
           color: Colors.black
         ),
        ),
        backgroundColor: Color.fromRGBO(250, 250, 250, 1),
      ),
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: YYBBRefreshHeadWidget(),
        footer: YYBBRefreshLoadingWidget(),
        controller: _refreshController,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: _childWitdget(),
      ),
    );
  }

  //  loading 加载
  Widget _childWitdget() {
    Widget _childWidget;
    if (_dataList != null && _dataList.length != 0) {
      _childWidget = ListView.builder(
        itemBuilder: _cellForRow,
        itemCount: _dataList.length > 0 ? _dataList.length : 0,
      );
    } else {
      _childWidget = new YYBBNODataWidget();
      // YYBBPlatform.invokeMapMethod("NodataCallBack");

    }
    return _childWidget;
  }

    // Cell 布局
    Widget _cellForRow(BuildContext context, int index ){
      YYBBAbnormalOrderModel _model = _dataList[index];
      YYBBAbnormalOrderProducts _item = _model.products.first;
      double transactionPrice = double.parse(_item.transactionPrice);
      _item.transactionPrice = transactionPrice.toStringAsFixed(2);
      String orderType = "采购";
      if(_model.proprietaryFlag == 3){
        orderType =  "团购";
      }
      return FlatButton(
        splashColor: Colors.transparent, // 点击时的高亮效果设置为透明
        onPressed: (){
          _pushOrderDetailPage(_model);
        },
        child: Column(
          // color: Colors.black38,
          children: [
            Container(
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        _model.recordTime,
                        style: TextStyle(
                          fontSize: 13,
                          color: YYBBColor153,
                        ),
                      ),
                      Text(
                        _model.abnormalTypeText,
                        style: TextStyle(
                          fontSize: 13,
                          color: YYBBColorOrange,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height:12,),
                  // 分割线
                  Container(
                    height:0.5,
                    color: YYBBLineColor,
                  ),
                  SizedBox(height: 12,),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children:[
                        Container(
                          child: Row(
                            children: [
                              Text(
                                _model.shortName ?? _model.enterpriseName,
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight:FontWeight.w700,
                                  color: YYBBColor51,
                                ),
                              ),
                              SizedBox(width: 10,),
                              Container(
                                constraints: BoxConstraints(
                                  minHeight: 10,
                                ),
                                decoration: BoxDecoration(
                                    color: YYBBColorBlue,
                                    borderRadius: BorderRadius.circular(3),
                                    ///圆角
                                    border: Border.all(color: Colors.white10, width: 1)
                                  ///边框颜色、宽
                                ),
                                // color:,
                                child: Text(
                                  " $orderType ",
                                  style:TextStyle(
                                    fontSize: 12,
                                    color: Colors.white
                                  ) ,
                                ),
                              ),
                              // Image(
                              //   image: new AssetImage("loging"),
                              // ),
                            ],
                          ),
                        ),
                        Text(
                          "销售员 | ${_model.sellerName}",
                          textAlign: TextAlign.right,
                          style: TextStyle(
                            fontSize: 14,
                            // fontWeight:FontWeight.w400,
                            color: YYBBColor153,
                          ),
                        )
                      ],
                    ),
                  ),
                  SizedBox(height: 12,),
                  // 中间视图
                  Container(
                    height: 72,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          child: Row(
                            children:[
                              Image(
                                image: new NetworkImage(_item.corrugatedTypeImg),
                                fit: BoxFit.cover,
                                width: 72,
                                height: 72,
                              ),
                              SizedBox(width: 12,),
                              Container(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      _item.materialCode+"(${_item.corrugatedType}楞)",
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        fontSize: 16,
                                        color: YYBBColor51,
                                      ),
                                    ),
                                    SizedBox(height: 12,),
                                    Text(
                                      "长*宽:${_item.sizeX}mm*${_item.sizeY}mm",
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: YYBBColor102,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ) ,
                        ),
                        Container(
                          alignment: Alignment.topCenter,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Text(
                                "￥${_item.transactionPrice}",
                                textAlign: TextAlign.right,
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight:FontWeight.w400,
                                  color: YYBBColorOrange,
                                ),
                              ),
                              SizedBox(height: 12,),
                              Text(
                                "数量:${_item.quantity}片",
                                textAlign: TextAlign.right,
                                style: TextStyle(
                                  fontSize: 12,
                                  color: YYBBColor102,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 12,),
                  // 分割线
                  Container(
                    height:0.5,
                    color: YYBBLineColor,
                  ),
                  // 底部按钮
                  Container(
                    padding: EdgeInsets.fromLTRB(0, 12, 0, 12),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Text(
                              "总金额: ",
                              style: TextStyle(
                                fontSize: 12,
                                color: YYBBColor113,
                              ),
                            ),
                            Text(
                              "¥${_model.totalProductPrice}",
                              style: TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.w800,
                                color: YYBBColorRed,
                              ),
                            ),],
                        ),
                        // OutlineButton(
                        //   onPressed: (){},  splashColor: Colors.transparent, // 点击时的高亮效果设置为透明
                        //   highlightColor: Colors.transparent, // 长按时的扩散效果设置为透明
                        //   shape:RoundedRectangleBorder(
                        //     borderRadius: BorderRadius.all(Radius.circular(4)),
                        //   ),
                        //   child: Text(
                        //     '查看物流',
                        //     style: TextStyle(
                        //       fontSize: 13.0,
                        //       color: YYBBColor119,
                        //     ),
                        //   ),
                        // ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            // 分割线flu
            Container(
              height:8,
              color: YYBBColor245,
            ),
          ],
        ),
      );
    }

    // 获取数据
    void _getData() async {
      var params = Map<String, String>();
      params["currentPage"] = _page.toString();
      params["pageSize"] = "10";
      params["searchKey"] = "";
      ApiResponse<YYBBAbnormalOrderModelResultData> item  = await AbnormalOrderVModel.getCategories(params);
      if(_page == 1) {
        _dataList.clear();
      }
      _dataList.addAll(item.data.list);
      _stopRefresh();
      // 刷新界面
      setState(() {});
    }


    // 点击进入下一个界面
    void _pushOrderDetailPage(YYBBAbnormalOrderModel model){
      // Map param = [];
      var params = Map<String, String>();
      params["orderID"] = model.orderId.toString();
      params["abnormalType"] = model.abnormalType.toString();

      FlutterBoost.singleton.open("abnormalOrderDetailPage",urlParams:params,exts: {"animated":true});

    }

    //下拉加载
    void _onRefresh() async{
      // await Future.delayed(Duration(milliseconds: 1000));
      _page = 1;
      _getData();
      setState(() {

      });
      // _refreshController.refreshCompleted();

    }

    //上拉加载
    void _onLoading() async{
      // monitor network fetch
      // await Future.delayed(Duration(milliseconds: 1000));
      _page++;
      _getData();
      if(mounted)
        setState(() {
        });
    }


    // 停止刷新
    void _stopRefresh(){

      if(_dataList.length  < 10 * _page){
        _refreshController.loadNoData();
      }else{
        _refreshController.loadComplete();
      }
      _refreshController.refreshCompleted();
    }
}




